package com.zxmr.domain.service;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zxmr.CommonInit;
import com.zxmr.db.DBManager;
import com.zxmr.domain.Consult;
import com.zxmr.domain.ConsultLevel;
import com.zxmr.domain.ConsultWay;

public class ConsultService {

	DBManager db = CommonInit.getDBManager();
	
	public boolean addConsultWay(ConsultWay way)
	{
		try
		{
			db.getMapper().insert("consult.addConsultWay", way);
			return true;
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return false;
	}
	public ConsultWay queryConsultWay(int id)
	{
		try
		{
			return (ConsultWay)db.getMapper().queryForObject("consult.querConsultWay", id);
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return null;
	}
	public boolean deleteConsultWay(int id)
	{
		try
		{
			db.getMapper().delete("consult.deleteConsultWay", id);
			return true;
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean updateConsultWay(ConsultWay way)
	{
		try
		{
			db.getMapper().update("consult.updateConsultWay", way);
			return true;
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return false;
	}
	@SuppressWarnings("unchecked")
	public List<ConsultWay> queryAllConsultWay(int company)
	{
		try
		{
			return (List<ConsultWay>)db.getMapper().queryForList("consult.queryAllConsultWay", company);
			
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return null;
	}
	
	public ConsultWay queryByNameAndCompany(String name,int company)
	{
		try
		{
			Map<String,Object> para = new HashMap<String,Object>();
			para.put("name", name);
			para.put("company", company);
			
			return (ConsultWay)db.getMapper().queryForObject("consult.queryCWByNameAndCompany", para);
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return null;
	}
	
	public boolean addConsult(Consult consult)
	{
		try
		{
			db.getMapper().insert("consult.add", consult);
			return true;
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return false;
	}
	public boolean updateConsult(Consult consult)
	{
		try
		{
			db.getMapper().update("consult.update", consult);
			return true;
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return false;
	}
	public Consult queryFullFieldConsult(long id)
	{
		
		try
		{
			return (Consult)db.getMapper().queryForObject("consult.queryFullField", id);
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return null;
	}
	public Consult querySimpleFiledConsult(long id)
	{
		try
		{
			return (Consult)db.getMapper().queryForObject("consult.querySimpleField", id);
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return null;
	}
	@SuppressWarnings("unchecked")
	public List<Consult> querySimpleFieldConsultByOpp(long opportunity)
	{
		try
		{
			return (List<Consult>)db.getMapper().queryForList("consult.querySimpleFieldByOpp", opportunity);
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return null;
	}
	@SuppressWarnings("unchecked")
	public List<Consult> querySimpleFieldConsultByCustomer(int customer)
	{
		try
		{
			return (List<Consult>)db.getMapper().queryForList("consult.querySimpleFieldByCus", customer);
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return null;
	}
	public Consult queryConsult(long id)
	{
		
		try
		{
			return (Consult)db.getMapper().queryForObject("consult.queryByID", id);
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return null;
	}
	public boolean addConsultLevel(ConsultLevel type)
	{
		try
		{
			db.getMapper().insert("consult.addConsultLevel",type);
			return true;
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return false;
	}
	public boolean deleteConsultLevel(int id)
	{
		try
		{
			db.getMapper().delete("consult.deleteConsultLevel", id);
			return true;
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return false;
	}
	public boolean updateConsultLevel(ConsultLevel type)
	{
		try
		{
			db.getMapper().delete("consult.updateConsultLevel", type);
			return true;
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return false;
	}
	public ConsultLevel queryConsultLevel(int id)
	{
		try
		{
			return (ConsultLevel)db.getMapper().queryForObject("consult.queryConsultLevel", id);
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return null;
	}
	@SuppressWarnings("unchecked")
	public List<ConsultLevel> queryAllConsultLevel(int company)
	{
		try
		{
			return (List<ConsultLevel>)db.getMapper().queryForList("consult.queryAllConsultLevel", company);
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<Consult> queryConsultPagination(ConsultSearchBean bean)
	{
		try {
			return (List<Consult>)db.getMapper().queryForList("consult.searchPagination", bean);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	public int queryConsultCount(ConsultSearchBean bean)
	{
		try {
			int id = (Integer)db.getMapper().queryForObject("consult.findCountPagination",bean);
			return id;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
	}
	public void deleteConsult(Consult consult)
	{
		try {
			db.getMapper().delete("consult.delete",consult);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
